/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame;

import com.jme3.asset.AssetManager;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.input.controls.ActionListener;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Sphere;
import controls.EnemyControl;

/**
 *
 * @author dani
 */
public class ShootingActionListener implements ActionListener {

    private Node shootables;
    private Camera cam;
    private Node rootNode;
    private Spatial mark;
    private AssetManager assetManager;
    private Sound sound;
    private long bulletCooldown = 0;

    public ShootingActionListener(Node shootables, Camera cam, Node rootNode, AssetManager assetManager, Sound sound) {
        this.shootables = shootables;
        this.cam = cam;
        this.rootNode = rootNode;
        this.assetManager = assetManager;
        this.sound = sound;
        initMark();
    }

    public void onAction(String name, boolean keyPressed, float tpf) {
        if (name.equals("Shoot") && keyPressed) {
            if (System.currentTimeMillis() - bulletCooldown > 125f) {
                sound.playerShoot();
                bulletCooldown = System.currentTimeMillis();

                CollisionResults results = new CollisionResults();
                Ray ray = new Ray(cam.getLocation(), cam.getDirection());
                shootables.collideWith(ray, results);
                System.out.println("----- Collisions? " + results.size() + "-----");
                for (int i = 0; i < results.size(); i++) {
                    // For each hit, we know distance, impact point, name of geometry.
                    float dist = results.getCollision(i).getDistance();
                    Vector3f pt = results.getCollision(i).getContactPoint();
                    String hit = results.getCollision(i).getGeometry().getName();
                    System.out.println("* Collision #" + i);
                    System.out.println("  You shot " + hit + " at " + pt + ", " + dist + " wu away." + results.getCollision(i).getGeometry().getParent().getName());
                }
                // 5. Use the results (we mark the hit object)

                if (results.size() > 0) {
                    // The closest collision point is what was truly hit:
                    CollisionResult closest = results.getClosestCollision();
                    // Let's interact - we mark the hit with a red dot.
                    mark.setLocalTranslation(closest.getContactPoint());
                    rootNode.attachChild(mark);

                    //
                    Spatial enemy = getEnemyParent(closest.getGeometry());
                    if (enemy != null) {
                        //if (closest.getGeometry().getParent().getName().equals("enemies")) {
//                        Spatial enemy = closest.getGeometry();
                        Integer hp = (Integer) enemy.getUserData("hp") - Main.PLAYER_DAMAGE;
                        enemy.setUserData("hp", hp);
                        System.out.println("Dealt " + Main.PLAYER_DAMAGE + " dmg to " + enemy.getName());
                    }
                } else {
                    // No hits? Then remove the red mark.
                    rootNode.detachChild(mark);
                }
            }
        }
    }

    /**
     * A red ball that marks the last spot that was "hit" by the "shot".
     */
    private void initMark() {
        Sphere sphere = new Sphere(30, 30, 0.2f);
        mark = new Geometry("BOOM!", sphere);
        Material mark_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mark_mat.setColor("Color", ColorRGBA.Red);
        mark.setMaterial(mark_mat);
    }

    private Spatial getEnemyParent(Spatial sp) {
        if (sp.getParent() == null) {
            return null;
        }
        if (sp.getName().equals("enemy")) {
            return sp;
        }
        if (sp.getParent().getName().equals("enemy")) {
            return sp.getParent();
        } else {
            return getEnemyParent(sp.getParent());
        }
    }
}